﻿using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Magic.Core.Service
{
    /// <summary>
    /// 数据库管理
    /// </summary>
    [ApiDescriptionSettings(Name = "DataBase", Order = 200)]
    public class DataBaseManager : IDataBaseManager, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarClient _sqlSugarClient;
        public DataBaseManager(ISqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        public void ColumnAdd(DbColumnInfoOutput input)
        {
            throw new NotImplementedException();
        }

        public void ColumnDelete(DbColumnInfoOutput input)
        {
            throw new NotImplementedException();
        }

        public void ColumnEdit(DbColumnInfoOutput input)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取表字段
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        [HttpGet("/dataBase/columnInfoList")]
        public List<DbColumnInfoOutput> GetColumnInfosByTableName(string tableName)
        {
            if (string.IsNullOrWhiteSpace(tableName))
                return new List<DbColumnInfoOutput>();
            return _sqlSugarClient.DbMaintenance.GetColumnInfosByTableName(tableName).Adapt<List<DbColumnInfoOutput>>();
        }

        /// <summary>
        /// 获取所有表
        /// </summary>
        /// <returns></returns>
        [HttpGet("/dataBase/tableInfoList")]
        public List<DbTableInfo> GetTableInfoList()
        {
            return _sqlSugarClient.DbMaintenance.GetTableInfoList();
        }
        /// <summary>
        /// 新增表
        /// </summary>
        /// <param name="input"></param>
        [HttpPost("/table/add")]
        public void TableAdd(DbTableInfo input)
        {
            List<DbColumnInfo> columnInfos = _sqlSugarClient.DbMaintenance.GetColumnInfosByTableName("dbtest");
            List<DbColumnInfo> c = new List<DbColumnInfo>();
            columnInfos.ForEach(m => {
                c.Add(new DbColumnInfo
                {
                    TableName = input.Name,
                    DbColumnName = m.DbColumnName,
                    DataType = m.DataType,
                    IsPrimarykey = m.IsPrimarykey,
                    IsIdentity = m.IsIdentity
                });
            });
            _sqlSugarClient.DbMaintenance.CreateTable(input.Name, c, false);
            _sqlSugarClient.DbMaintenance.AddTableRemark(input.Name, input.Description);
            return;
        }
        /// <summary>
        /// 删除表
        /// </summary>
        /// <param name="input"></param>
        [HttpPost("/table/delete")]
        public void TableDelete(DbTableInfo input)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 编辑表
        /// </summary>
        /// <param name="input"></param>
        [HttpPost("/table/edit")]
        public void TableEdit(DbTableInfo input)
        {
            throw new NotImplementedException();
        }
    }
}
